package com.example.Sao.Tools;

import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import com.example.Sao.Data.DepartmentList;
import com.google.gson.Gson;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: scan
 * @Package: com.example.Sao.Tools
 * @ClassName: DepartmentFileutil
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2020/5/26 14:31
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/5/26 14:31
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class DepartmentFileutil {
    private final static String Path  = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+"DepartmentJson";
    private static DepartmentList departmentList  =null;

    public static void SaveDepartment(DepartmentList departmentList){
           File file = new File(Path);
           if (!file.exists()){
               file.mkdirs();
           }
               Observable.create(new ObservableOnSubscribe<String>() {
                   @Override
                   public void subscribe(ObservableEmitter<String> e) throws Exception {
                       File file1 = new File(Path + "/department.json");
                       if (file1.exists()){
                           file1.delete();
                           file1.createNewFile();
                       }
                       OutputStream out = new FileOutputStream(Path + "/department.json");
                       out.write(new Gson().toJson(departmentList).getBytes("UTF-8"));
                       out.close();
                       e.onNext("succes");
                   }
               }).subscribeOn(Schedulers.newThread())
                       .observeOn(AndroidSchedulers.mainThread())
                       .subscribe(new Consumer<String>() {
                           @Override
                           public void accept(String file) throws Exception {
                               Log.d("Save","保存成功");
                           }
                       });
    }

    public static Observable GetDepartment() {
        return Observable.create(new ObservableOnSubscribe<DepartmentList>() {
            @Override
            public void subscribe(ObservableEmitter<DepartmentList> e){
                try {
                    File file = new File(Path + "/department.json");
                    if (file.exists()) {
                        InputStream in = new FileInputStream(Path + "/department.json");
                        if (in != null) {
                            int temp = 0;
                            byte buffer[] = new byte[1024 * 16];
                            while ((temp = in.read(buffer)) != -1) {
                                if (in.available() <= 0) {
                                    byte[] data = new byte[temp];
                                    System.arraycopy(buffer, 0, data, 0, temp);
                                    DepartmentList departmentList = new Gson().fromJson(new String(data, "UTF-8"), DepartmentList.class);
                                    Log.d("Department", "文件数据" + new Gson().toJson(departmentList));
                                    e.onNext(departmentList);
                                    in.close();
                                }
                            }
                        }
                    }else {
                        DepartmentList departmentList = new DepartmentList();
                        List<String>en = new ArrayList<>();
                        departmentList.setDepartmentList(en);
                        departmentList.setNum(0);
                        e.onNext(departmentList);
                    }
                }catch (IOException e3){
                    e3.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());
    }
}
